﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.Logging;
using System.Diagnostics;
using SpringNetCheckerCore;

namespace SpringNetObjectsChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            //檢查輸入值
            if (args.Length < 2)
            {
                Console.WriteLine(@"請輸入 參數1:[Spring Objects Xml 設定檔位置] 參數2:[使用到的組件檔資料夾位置]");
                Console.WriteLine(@"ex. SpringNetObjectsChecker C:\web\objects.xml C:\web\bin");
                Console.WriteLine(@"Press Any Key ..." + "\n");
                Console.ReadKey();
                return;
            }

            //初始化記錄檔
            FileLogTraceListener tempListener = new FileLogTraceListener();
            tempListener.BaseFileName = string.Format("Log-{0:yyyy-MM-dd-HHmmss}", DateTime.Now);
            tempListener.Location = LogFileLocation.Custom;
            tempListener.CustomLocation = Environment.CurrentDirectory + @"\Log";
            //tempListener.LogFileCreationSchedule = LogFileCreationScheduleOption.Daily;
            tempListener.AutoFlush = true;
            tempListener.Append = true;
            Trace.Listeners.Add(tempListener);

            //新增檢查者
            string springObjectsSchemaFilePath = Properties.Settings.Default.SpringObjectsSchemaFilePath;
            string springDatabaseSchemaFilePath = Properties.Settings.Default.SpringDatabaseSchemaFilePath;

            ISpringNetObjectsChecker springNetObjectsChecker =
                new SpringNetCheckerCore.SpringNetObjectsChecker(springObjectsSchemaFilePath, springDatabaseSchemaFilePath);

            //掛上寫Log的方法
            springNetObjectsChecker.AddLogWriteLineDelegate(new LogWriteLineDelegate(LogWriteLine));

            string objectsXmlFilePath = args[0];
            string assembliesDirectoryPath = args[1];
            //檢查 Spring Objects Xml 設定檔
            bool result = springNetObjectsChecker.CheckObjectsXmlFile(objectsXmlFilePath, assembliesDirectoryPath);

            tempListener.Flush();
        }

        /// <summary>
        /// 顯示並記錄訊息
        /// </summary>
        static void LogWriteLine(string value)
        {
            //Trace.TraceInformation("{0:yyyy/MM/dd HH:mm:ss}\t{1}", DateTime.Now, value);
            Trace.WriteLine(string.Format("{0:yyyy/MM/dd HH:mm:ss}\t{1}", DateTime.Now, value));
            Console.WriteLine(value);
        }
    }
}
